package com.niiwoo.civet.mobile.vo.response.credit;

import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.dubbo.common.utils.Assert;
import com.niiwoo.civet.trade.constant.TradeCommonConstant;
import com.niiwoo.civet.trade.dto.response.credit.InvestigationProjectDTO;
import com.niiwoo.civet.trade.enums.credit.CreditLoanGuaranteeProjectStatusEnum;
import com.niiwoo.civet.trade.enums.credit.GuaranteeStatusEnum;
import com.niiwoo.civet.trade.enums.credit.InputInvestigationStatusEnum;
import com.niiwoo.civet.trade.enums.credit.InvestigationStatusEnum;
import com.niiwoo.tripod.base.utils.DateUtils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
@ApiModel("尽调项目信息")
public class InvestigationProjectResponseVO {

    @ApiModelProperty("项目编号")
    private String projectId;
	
    @ApiModelProperty("借款人用户编号")
    private String borrowUserId;

    @ApiModelProperty("借款人昵称")
    private String borrowNickName;
    
    @ApiModelProperty("借款人头像")
    private String borrowHeadImg;
    
    @ApiModelProperty("借款人性别 0、未知 1、男 2、女")
    private String borrowSex;

    @ApiModelProperty("项目标题")
    private String title;

    @ApiModelProperty("合同金额")
    private String contractAmount;
    
    @ApiModelProperty("担保费率")
    private String guaranteeRate;
    
    @ApiModelProperty("期数")
    private String deadline;

    @ApiModelProperty("状态 说明 1：已结束； 2：流标")
    private String status;
    
    @ApiModelProperty("城市")
    private String loanCity;
    
    @ApiModelProperty("区域")
    private String loanArea;

    @ApiModelProperty("发布时间")
    private String createTime;
    
    /**
     * DTO 转化成 VO
     * 
     * @param dto
     * @return
     */
    public static InvestigationProjectResponseVO convertDTO2VO(InvestigationProjectDTO dto) {
    	Assert.notNull(dto, new IllegalArgumentException("请求转化的DTO对象为空"));
    	InvestigationProjectResponseVO vo = new InvestigationProjectResponseVO();
    	vo.setProjectId(objectToString(dto.getProjectId()));
    	vo.setBorrowUserId(objectToString(dto.getBorrowUserId()));
    	vo.setBorrowNickName(dto.getBorrowNickName());
    	vo.setBorrowHeadImg(dto.getBorrowHeadImg());
    	vo.setBorrowSex(objectToString(dto.getBorrowSex()));
    	vo.setTitle(dto.getTitle());
    	vo.setContractAmount(objectToString(dto.getContractAmount()));
    	vo.setGuaranteeRate(objectToString(dto.getGuaranteeRate()));
    	vo.setDeadline(objectToString(dto.getDeadline()));
    	vo.setStatus(bulidStatus(dto));
    	
		vo.setLoanCity(dto.getLoanCity());
		vo.setLoanArea(dto.getLoanArea());
		vo.setCreateTime(objectToString(dto.getCreateTime()));
    	return vo;
    }

    /**
     * 对象转化成字符串
     * 
     * @param object
     * @return
     */
    private static String objectToString(Object object) {
    	if (null == object) return "";
    	
    	if (object instanceof BigDecimal) {
    		return ((BigDecimal) object).setScale(TradeCommonConstant.NUMBER_PRECISION_TWO, BigDecimal.ROUND_HALF_EVEN).toString();
    	}
    	
    	if (object instanceof Date) {
    		return DateUtils.format(((Date) object), DateUtils.SHOW_YEAR_MONTH_DATE_HOUR_SECOND);
    	}
    	
    	return object.toString();
    } 
    
    /**
     * 构建项目尽调状态 
     * 
     * 根据尽调人的担保状态和尽调状态，判断前断显示状态值
     * 
     * 状态说明 0：尽调中； 1：已结束； 2：流标；
     * 
     * @param dto
     * @return
     */
	private static String bulidStatus(InvestigationProjectDTO dto) {
		byte putInvestigationStatus = InputInvestigationStatusEnum.INVESTIGATION_OVER.getCode(); // 默认已结束

		Byte invegationstatus = (null != dto.getInvestigationStatus()) ? dto.getInvestigationStatus() : InvestigationStatusEnum.INVESTIGATION_PROGRESS.getCode();
		Byte guaranteestatus = (null != dto.getGuaranteeStatus()) ? dto.getGuaranteeStatus() : GuaranteeStatusEnum.GUARANTEE_PROGRESS.getCode();
		
		// 尽调流标判断逻辑：尽调状态：放弃尽调、超时未尽调 || 担保状态：放弃担保、撤销担保、担保超时）
		if (InvestigationStatusEnum.INVESTIGATION_GIVEUP.getCode() <= invegationstatus || GuaranteeStatusEnum.GUARANTEE_GIVEUP.getCode() <= guaranteestatus) {
			putInvestigationStatus = InputInvestigationStatusEnum.INVESTIGATION_FLOW.getCode();
		}
		
		// 其它的都标示：已结束；进行中、尽调完成的
		return objectToString(putInvestigationStatus);
	}
	
	public static void main(String[] args) {
		InvestigationProjectDTO dto = new InvestigationProjectDTO();
		
		dto.setProjectStatus(CreditLoanGuaranteeProjectStatusEnum.INVESTIGATING.getCode());
		dto.setInvestigationStatus(InvestigationStatusEnum.INVESTIGATION_GIVEUP.getCode());
		dto.setGuaranteeStatus(GuaranteeStatusEnum.GUARANTEE_PROGRESS.getCode());
		
		System.out.println(bulidStatus(dto));
	}
}